package com.hutiao.snowhb.actions.wechat;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.interceptor.ServletRequestAware;

import com.hutiao.snowhb.common.MongoUtil;
import com.hutiao.snowhb.wechat.tools.WeiXinTools;
import com.hutiao.snowhb.wechat.vo.WxRecvEventGeo;
import com.hutiao.snowhb.wechat.vo.WxRecvEventSubscribe;
import com.hutiao.snowhb.wechat.vo.WxSendMsg;
import com.hutiao.snowhb.wechat.vo.WxSendTextMsg;
import com.opensymphony.xwork2.ActionSupport;

public class HandleEventMsgGeoAction extends ActionSupport implements ServletRequestAware {

	private static final long serialVersionUID = 5957191867881783774L;
	
	private static Logger logger = Logger.getLogger(HandleEventMsgGeoAction.class);
	
	private HttpServletRequest request;
	
	private String result;

	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}
	
	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}


	@Action(value="handleeventgeo",
			results = {
				@Result(name="success",type="xmlstr", params={"stringName", "result"})})
	public String execute() throws Exception {
		
		logger.info("msg type is WxRecvEventGeo");
		WxRecvEventGeo geoEvent = (WxRecvEventGeo) this.request.getAttribute("recvMsg");
		
		logger.info("geoEvent.latitude=" + geoEvent.getLatitude());
		logger.info("geoEvent.longitude=" + geoEvent.getLongitude());
		logger.info("geoEvent.precision=" + geoEvent.getPrecision());
		
		
//		WxSendMsg sendMsg = WeiXinTools.builderSendByRecv(geoEvent);
		
//		StringBuffer buffer = new StringBuffer();
//		buffer.append("Event:" + geoEvent.getEvent() ).append("\n");
//		buffer.append("Latitude:" + geoEvent.getLatitude() ).append("\n");
//		buffer.append("Longitude:" + geoEvent.getLongitude() ).append("\n");
//		buffer.append("Precision:" + geoEvent.getPrecision() ).append("\n");
//		
//		sendMsg = new WxSendTextMsg(sendMsg, buffer.toString());
//		this.result = WeiXinTools.getSendXmlStr(sendMsg);
		
		if (MongoUtil.findUserLocal(geoEvent.getFromUser()) != null) {
			MongoUtil.removeUserLocal(geoEvent.getFromUser());
		}
		MongoUtil.saveUserLocal(geoEvent);
		
		return SUCCESS;
	}

}
